Socket
Socket
Sign inDemoInstall

require-in-the-middle

Package Overview
Dependencies
Maintainers
2
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

require-in-the-middle

Module to hook into the Node.js require function


Version published
Maintainers
2
Created

What is require-in-the-middle?

The require-in-the-middle package allows for the interception and modification of Node.js module loading. This can be particularly useful for instrumentation, logging, or modifying module behavior at runtime without altering the original module code.

What are require-in-the-middle's main functionalities?

Intercepting module loading

This feature allows you to intercept the loading of specific modules (e.g., 'http') and execute custom logic, such as logging when a module is loaded. The callback function receives the exports of the module, the name of the module, and the base directory.

const hook = require('require-in-the-middle');

hook(['http'], { internals: true }, (exports, name, basedir) => {
  console.log(`Module loaded: ${name}`);
  return exports;
});

Modifying module exports

This demonstrates how to modify the exports of a module, in this case, 'express'. It wraps the original express function in a new function that logs a message every time it is called before proceeding with the original behavior.

const hook = require('require-in-the-middle');

hook(['express'], (exports, name) => {
  const originalFunction = exports;
  function modifiedFunction() {
    console.log('Express function called');
    return originalFunction.apply(this, arguments);
  }
  return modifiedFunction;
});

Other packages similar to require-in-the-middle

Keywords

FAQs

Package last updated on 25 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc